using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using SnipMe.Model;

namespace SnipMe.Model.Models
{ 
    public class CodeSnippetRepository : ICodeSnippetRepository
    {
        SnipMeModelContext context = new SnipMeModelContext();

        public IEnumerable<CodeSnippet> GetAllCodeSnippets(params Expression<Func<CodeSnippet, object>>[] includeProperties)
        {
            IQueryable<CodeSnippet> query = context.CodeSnippets;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query.ToList();
        }

        public CodeSnippet GetById(int id)
        {
            return context.CodeSnippets.Find(id);
        }

        public void InsertOrUpdate(CodeSnippet codesnippet)
        {
            if (codesnippet.Id == 0) {
                // New entity
                context.CodeSnippets.Add(codesnippet);
            } else {
                // Existing entity
                context.CodeSnippets.Attach(codesnippet);
                context.Entry(codesnippet).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var codesnippet = context.CodeSnippets.Find(id);
            context.CodeSnippets.Remove(codesnippet);
        }

        public void Save()
        {
            context.SaveChanges();
        }
    }

	public interface ICodeSnippetRepository
    {
		IEnumerable<CodeSnippet> GetAllCodeSnippets(params Expression<Func<CodeSnippet, object>>[] includeProperties);
		CodeSnippet GetById(int id);
		void InsertOrUpdate(CodeSnippet codesnippet);
        void Delete(int id);
        void Save();
    }
}